package com.apcc4m.sfpushbatch.writer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import com.apcc4m.sfpushbatch.repository.Notification;
import com.apcc4m.sfpushbatch.repository.NotificationRepository;
import com.apcc4m.sfpushbatch.repository.PushStatus;
import com.apcc4m.sfpushbatch.repository.PushStatusRepository;
import com.apcc4m.sfpushbatch.repository.User;
import com.apcc4m.sfpushbatch.repository.UserRepository;
import com.apcc4m.sfpushbatch.service.PushService;

public class PushStatusItemWriter<T> implements ItemWriter<PushStatus> {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private NotificationRepository notificationRepository;
    @Autowired
    private PushStatusRepository pushStatusRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PushService pushService;

    @Override
    public void write(List<? extends PushStatus> items) throws Exception {
        System.out.println("开始推送，数据量：" + items.size());
        Long t1 = System.currentTimeMillis();
        Notification notif = notificationRepository.findOneById(items.get(0).getnId());
        List<String> deviceIdList = new ArrayList<String>();
        for (PushStatus pushStatus : items) {
            deviceIdList.add(pushStatus.getDeviceId());
            pushStatus.setStatus(1);
        }
//        pushService.push(notif, deviceIdList);
        pushStatusRepository.save(items);
        Long t2 = System.currentTimeMillis();
        System.out.println("结束推送,花费时间(毫秒)" + (t2 - t1));
    }

}
